package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.OtherSafeguard2ndService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.OtherSafeguard2ndDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing OtherSafeguard2nd.
 */
@RestController
@RequestMapping("/api")
public class OtherSafeguard2ndResource {

    private final Logger log = LoggerFactory.getLogger(OtherSafeguard2ndResource.class);

    private static final String ENTITY_NAME = "otherSafeguard2nd";

    private final OtherSafeguard2ndService otherSafeguard2ndService;

    public OtherSafeguard2ndResource(OtherSafeguard2ndService otherSafeguard2ndService) {
        this.otherSafeguard2ndService = otherSafeguard2ndService;
    }

    /**
     * POST  /other-safeguard-2-nds : Create a new otherSafeguard2nd.
     *
     * @param otherSafeguard2ndDTO the otherSafeguard2ndDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new otherSafeguard2ndDTO, or with status 400 (Bad Request) if the otherSafeguard2nd has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/other-safeguard-2-nds")
    @Timed
    public ResponseEntity<OtherSafeguard2ndDTO> createOtherSafeguard2nd(@RequestBody OtherSafeguard2ndDTO otherSafeguard2ndDTO) throws URISyntaxException {
        log.debug("REST request to save OtherSafeguard2nd : {}", otherSafeguard2ndDTO);
        if (otherSafeguard2ndDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new otherSafeguard2nd cannot already have an ID")).body(null);
        }
        OtherSafeguard2ndDTO result = otherSafeguard2ndService.save(otherSafeguard2ndDTO);
        return ResponseEntity.created(new URI("/api/other-safeguard-2-nds/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /other-safeguard-2-nds : Updates an existing otherSafeguard2nd.
     *
     * @param otherSafeguard2ndDTO the otherSafeguard2ndDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated otherSafeguard2ndDTO,
     * or with status 400 (Bad Request) if the otherSafeguard2ndDTO is not valid,
     * or with status 500 (Internal Server Error) if the otherSafeguard2ndDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/other-safeguard-2-nds")
    @Timed
    public ResponseEntity<OtherSafeguard2ndDTO> updateOtherSafeguard2nd(@RequestBody OtherSafeguard2ndDTO otherSafeguard2ndDTO) throws URISyntaxException {
        log.debug("REST request to update OtherSafeguard2nd : {}", otherSafeguard2ndDTO);
        if (otherSafeguard2ndDTO.getId() == null) {
            return createOtherSafeguard2nd(otherSafeguard2ndDTO);
        }
        OtherSafeguard2ndDTO result = otherSafeguard2ndService.save(otherSafeguard2ndDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, otherSafeguard2ndDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /other-safeguard-2-nds : get all the otherSafeguard2nds.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of otherSafeguard2nds in body
     */
    @GetMapping("/other-safeguard-2-nds")
    @Timed
    public ResponseEntity<List<OtherSafeguard2ndDTO>> getAllOtherSafeguard2nds(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of OtherSafeguard2nds");
        Page<OtherSafeguard2ndDTO> page = otherSafeguard2ndService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/other-safeguard-2-nds");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /other-safeguard-2-nds/:id : get the "id" otherSafeguard2nd.
     *
     * @param id the id of the otherSafeguard2ndDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the otherSafeguard2ndDTO, or with status 404 (Not Found)
     */
    @GetMapping("/other-safeguard-2-nds/{id}")
    @Timed
    public ResponseEntity<OtherSafeguard2ndDTO> getOtherSafeguard2nd(@PathVariable Long id) {
        log.debug("REST request to get OtherSafeguard2nd : {}", id);
        OtherSafeguard2ndDTO otherSafeguard2ndDTO = otherSafeguard2ndService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(otherSafeguard2ndDTO));
    }

    /**
     * DELETE  /other-safeguard-2-nds/:id : delete the "id" otherSafeguard2nd.
     *
     * @param id the id of the otherSafeguard2ndDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/other-safeguard-2-nds/{id}")
    @Timed
    public ResponseEntity<Void> deleteOtherSafeguard2nd(@PathVariable Long id) {
        log.debug("REST request to delete OtherSafeguard2nd : {}", id);
        otherSafeguard2ndService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/other-safeguard-2-nds?query=:query : search for the otherSafeguard2nd corresponding
     * to the query.
     *
     * @param query the query of the otherSafeguard2nd search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/other-safeguard-2-nds")
    @Timed
    public ResponseEntity<List<OtherSafeguard2ndDTO>> searchOtherSafeguard2nds(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of OtherSafeguard2nds for query {}", query);
        Page<OtherSafeguard2ndDTO> page = otherSafeguard2ndService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/other-safeguard-2-nds");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * DELETE  /other-safeguard-2-nds/:id : delete the "id" otherSafeguard2nd.
     *
     * @param id the id of the otherSafeguard2ndDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @GetMapping("/other-safeguard-2-nds-By/{id}")
    @Timed
    public ResponseEntity<List<OtherSafeguard2ndDTO>> getAllOtherSafeguard2ndByProductId(@PathVariable Long id,@ApiParam Pageable pageable) {
        log.debug("REST request to delete OtherSafeguard2nd : {}", id);
        return new ResponseEntity<>(otherSafeguard2ndService.getAllProductId(id,pageable).getContent(),HttpStatus.OK);
    }

}
